﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
	Layout = null;
}

<link href="~/scripts/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />
<body>
	<h2>显示角色</h2>
	@Html.AntiForgeryToken()
	<button class="layui-btn" id="addRoleBtn">添加角色</button>
	<table class="layui-table">
		<thead>
			<tr>
				<th>ID</th>
				<th>角色名</th>
				<th>状态</th>
				<th>操作</th>
			</tr>
		</thead>
		<tbody id="tb"></tbody>
	</table>
</body>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="~/scripts/layui-v2.5.6/layui/layui.js"></script>
<script>
	$(function () {
	   show();
	});
	function show() {
		$.ajax({
		 url:"/Role/GetRoles?_=" + new Date().getTime(),
		 type:"get",
		 dataType:"json",
		 success:function (res) {
			 console.log(res);
			 $("#tb").empty();
			 var data = res.Data || res;
			 if (Array.isArray(data) && data.length > 0) {
				 $.each(data,function(index,item){
					 var statusText = item.roleStatus === 1 ? "启用" : "禁用";
					 var row=`
					 <tr>
						 <td>${item.id}</td>
						 <td>${item.roleName}</td>>
						 <td>${statusText}</td>
						 <td>
						 <button class="layui-btn layui-btn-normal layui-btn-sm edit-btn" onclick="editRole(${item.id})">编辑</button>
						 <button class="layui-btn layui-btn-danger layui-btn-sm delete-btn" onclick="deleteRole(${item.id})">删除</button>
						  <button class="layui-btn layui-btn-warm layui-btn-sm delete-btn" onclick="rolepermission(${item.id})">角色分配权限</button>
						 </td>
					 </tr>
					 `;
					 $("#tb").append(row);
				 });
			 } else {
				 $("#tb").append('<tr><td colspan="3" style="text-align:center">暂无数据</td></tr>');
			 }
		 },
		})
	}

	//添加角色按钮单击处理程序
	$('#addRoleBtn').on('click', function() {
		layer.open({
			type: 2, // iframe type
			title: '添加角色',
			area: ['500px', '350px'], // Adjust size as needed
			content: '/Role/Roles',
			end: function(){
				show(); // Refresh table after popup is closed
			}
		});
	});

	//删除角色
	function deleteRole(id){
		layer.confirm('确定要删除该角色?',{
			btn:['确认','取消']
		},function(){
			var token = $('input[name="__RequestVerificationToken"]').val(); // Get the anti-forgery token
			$.ajax({
				url:'/Role/DeleteRole?id=' + id, // Pass id as a query parameter
				type:'POST', // Use POST for deletion
				headers: {
					'RequestVerificationToken': token // Include token in headers
				},
				success: function (res) {
					layer.msg('删除成功', { icon: 1 });
					show(); // 刷新列表
				},
				error: function (xhr, status, error) {
					// 保留原有的错误处理逻辑
					if (xhr.status === 400) {
						alert('删除失败：未找到要删除的用户');
					} else if (xhr.status === 403) {
						alert('删除失败：没有删除权限');
					} else {
						alert('删除失败：' + (xhr.responseText || '服务器错误'));
					}
				}
			});
		});
	}

	// 编辑角色
	function editRole(id){
		layer.open({
			type: 2, // iframe type
			title: '编辑角色',
			area: ['500px', '350px'], // Adjust size as needed
			content: '/Role/EditRole?id=' + id, // Point to the new edit role view
			end: function(){
				show(); // Refresh table after popup is closed
			}
		});
	}
	//角色分配权限
	function rolepermission(id){
		window.location.href = '/Role/RPIndex?roleId=' + id;
	}




</script>
